﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Alex.Library.Cloud.Libs;

namespace Alex.Library.Cloud.VMWare.V51.VMMParameters
{
    public struct CreateItemType
    {
        public const string StandaloneHost = "Host-Standalone";
        public const string Cluster = "Cluster";
        public const string Datacenter = "Datacenter";
        public const string Folder = "Folder";
    }

    public class CreateParameter : IVMMParameter
    {
        public CreateParameter(string parentName, string itemType, string itemName)
        {
            this.ParentName = parentName;
            this.ItemType = itemType;
            this.ItemName = itemName;
            this.OtherProps = new Dictionary<string, object>();
        }

        public CreateParameter()
            : this(string.Empty, string.Empty, string.Empty)
        {

        }

        public string ParentName { get; set; }
        public string ItemType { get; set; }
        public string ItemName { get; set; }
        public Dictionary<string, object> OtherProps { get; private set; }

        public bool IsValid()
        {
            bool isvalid = true;
            StringBuilder sb = new StringBuilder();
            if (String.IsNullOrEmpty(ParentName))
            {
                sb.AppendLine("ParentName is empty or null.");
                isvalid = false;
            }
            if (String.IsNullOrEmpty(ItemType))
            {
                sb.AppendLine("ItemType is empty or null.");
                isvalid = false;
            }
            if (!ItemType.Equals(CreateItemType.StandaloneHost, StringComparison.OrdinalIgnoreCase)
                && !ItemType.Equals(CreateItemType.Cluster, StringComparison.OrdinalIgnoreCase)
                && !ItemType.Equals(CreateItemType.Datacenter, StringComparison.OrdinalIgnoreCase)
                && !ItemType.Equals(CreateItemType.Folder, StringComparison.OrdinalIgnoreCase))
            {
                sb.AppendLine("ItemType is not valid.");
                isvalid = false;
            }

            if (String.IsNullOrEmpty(ItemName))
            {
                sb.AppendLine("ItemName is empty or null.");
                isvalid = false;
            }

            if (ItemType == CreateItemType.StandaloneHost)
            {
                if (!OtherProps.ContainsKey("HostUserName"))
                {
                    sb.AppendLine("ItemType is Host-Standalone, but OtherProps does not contains 'HostUserName'.");
                    isvalid = false;
                }
                if (!OtherProps.ContainsKey("HostPassword"))
                {
                    sb.AppendLine("ItemType is Host-Standalone, but OtherProps does not contains 'HostPassword'.");
                    isvalid = false;
                }
                if (!OtherProps.ContainsKey("HostPort"))
                {
                    sb.AppendLine("ItemType is Host-Standalone, but OtherProps does not contains 'HostPort'.");
                    isvalid = false;
                }
            }
            ErrorMessage = sb.ToString();
            return isvalid;
        }

        public string ErrorMessage { get; private set; }
    }
}
